#!/bin/bash

version=4.2.1.240230938-experimental

if [ $arch == 32 ];then
  url_arch=armv7l
elif [ $arch == 64 ];then
  url_arch=aarch64
else
  error "Failed to detect OS CPU architecture! Something is very wrong."
fi

if package_installed musescore ;then
  status "First removing musescore package with APT..."
  sudo apt purge musescore -y --autoremove || exit 1
fi

enable_module fuse || exit 1

wget -O "/tmp/MuseScore.AppImage" "https://github.com/musescore/MuseScore/releases/download/v${version%.*}/MuseScore-${version}-${url_arch}.AppImage" || exit 1

chmod +x "/tmp/MuseScore.AppImage"

#Use built-in install procedure
sudo /tmp/MuseScore.AppImage install || error "MuseScore.AppImage failed to run its own installation procedure. Please review errors above."
#this moves itself to /usr/local/bin/MuseScore.AppImage

#make terminal command
sudo rm -f /usr/local/bin/mscore
sudo ln -s /usr/local/bin/MuseScore.AppImage /usr/local/bin/mscore

#change menu button label to 'MuseScore 4'
sudo sed -i 's/^Name=.*/Name=MuseScore 4/g' /usr/local/share/applications/org.musescore.MuseScore4portable.desktop
true #in case sed fails
